<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商家-商品管理</title>
    <link rel="stylesheet" href="../css/base.css">
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
</head>
<body>
<div id="app">
    <div style=" height: 60px; background-color: #8cc5ff; margin-bottom: 20px;">
        <h2 style="line-height: 60px; margin-left: 230px; float: left">商家-商品管理</h2>
        <h2 style="line-height: 60px; margin-right: 230px; float: right; cursor: pointer">关于我</h2>
    </div>
    <el-input clearable v-model="spoonName" placeholder="请输入汤勺名称" style="width: 200px;margin-left: 20px;"></el-input>
    <el-button type="primary" class="butT" @click="handleSearch()">查询</el-button>
    <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
    <p>&nbsp;</p>
    <el-table
            :data="commodityList"
            style="width: 100%">
        <el-table-column
                type="selection"
                width="60">
        </el-table-column>
        <el-table-column
                type="index"
                label="序号"
                width="60">
        </el-table-column>
        <el-table-column
                prop="name"
                label="商品名称"
                width="280">
        </el-table-column>
        <el-table-column
                prop="price"
                label="价格（元/件）"
                width="280">
        </el-table-column>
        <el-table-column
                prop="inventory"
                label="库存（件）"
                width="280">
        </el-table-column>
        <el-table-column
                prop="volume"
                label="销量（件）"
                width="280">
        </el-table-column>
        <!--<el-table-column
                prop="image"
                label="图片"
                width="280">
            <img :src="commodity.image" style="width: 200px; height: 200px;"/>
        </el-table-column>-->
        <el-table-column
                label="操作"
                width="280">
            <template slot-scope="scope">
                <el-button @click="handleLook(scope.row)" type="text">查看</el-button>
                <el-button @click="handleUpdate(scope.row)" type="text">编辑</el-button>
                <el-button @click="handleDelete(scope.row)" type="text">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!--查看框 start-->
    <el-dialog title="查看详情" :visible.sync="lookDialogFormVisible">
        <el-form ref="dataEditForm" :model="commodity" :rules="rules" label-position="right" label-width="100px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="名称" prop="name">
                        {{commodity.name}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="价格" prop="price">
                        ￥{{commodity.price}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="库存" prop="inventory">
                        {{commodity.inventory}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="销量" prop="volume">
                        {{commodity.volume}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="图片" prop="image">
                        <img :src="commodity.image" style="width: 200px; height: 200px;"/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="lookDialogFormVisible = false">取消</el-button>
        </div>
    </el-dialog>
    <!--查看框 end-->

    <!--编辑框 start-->
    <el-dialog title="编辑" :visible.sync="editDialogFormVisible">
        <el-form ref="dataEditForm" :model="commodity" :rules="rules" label-position="right" label-width="100px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="commodity.name"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="价格" prop="price">
                        <el-input v-model="commodity.price"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="库存" prop="inventory">
                        <el-input v-model="commodity.inventory"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="销量" prop="volume">
                        {{commodity.volume}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="图片" prop="image">

                        <!-- 文件上传元素 -->
                        <el-upload
                                class="avatar-uploader"
                                action="http://localhost:8080/api/commodity/upload-image"
                                :on-success="handleSuccess"
                        >
                            <el-button slot="trigger" size="small" type="primary">选择文件</el-button>
                            <!--用图片链接显示图片-->
                            <img :src="commodity.image" style="width: 200px; height: 200px;"/>
                        </el-upload>
                        <!--                        <el-upload-->
                        <!--                                ref="upload"-->
                        <!--                                class="avatar-uploader"-->
                        <!--                                :show-file-list="false"-->
                        <!--                                @change="handleImageChange"-->
                        <!--                                :on-success="handleSuccess"-->
                        <!--                                action="http://localhost:8080/api/commodity/upload-image"-->
                        <!--                        >-->
                        <!--                            <el-button slot="trigger" size="small" type="primary">选择文件</el-button>-->
                        <!--                        </el-upload>-->
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="editDialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="handleEdit()">确定</el-button>
        </div>
    </el-dialog>
    <!--编辑框 end-->

    <!--新建框 start-->
    <el-dialog title="新建" :visible.sync="addDialogFormVisible" >
        <el-form ref="dataEditForm" :model="commodity" :rules="rules" label-position="right" label-width="100px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="commodity.name"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="价格" prop="price">
                        <el-input v-model="commodity.price"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="库存" prop="inventory">
                        <el-input v-model="commodity.inventory"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="图片" prop="image">
                        <el-upload
                                class="avatar-uploader"
                                action="http://localhost:8080/api/commodity/upload-image"
                                :on-success="handleSuccess"
                        >
                            <el-button slot="trigger" size="small" type="primary">选择文件</el-button>
                            <!--用图片链接显示图片-->
                            <img  :src="commodity.image" style="width: 200px; height: 200px;"/>
                        </el-upload>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="addDialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="handleAdd()">确定</el-button>
        </div>
    </el-dialog>
    <!--新建框 end-->

    <!--商家(管理员) 信息-->
    <el-dialog title="编辑" :visible.sync="aboutMFormVisible">
        <el-form ref="dataEditForm" :model="merchant" :rules="rules" label-position="right" label-width="100px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="merchant.name"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="merchant.password"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="电话" prop="phone">
                        <el-input v-model="merchant.phone"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="地址" prop="address">
                        <el-input v-model="merchant.address"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12" style="display: none">
                    <el-form-item label="ID" prop="merchantId">
                        {{merchant.merchantId}}
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="aboutMFormVisible = false">取消</el-button>
            <el-button type="primary" @click="updateM()">确定</el-button>
        </div>
    </el-dialog>
    <!---->
</div>
</body>

<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            username: localStorage.getItem("username"),
            commodity: {},
            commodityList: [],
            spoonName: "",                  //汤勺名称，用于查找
            lookDialogFormVisible: false,   //查看框
            editDialogFormVisible: false,   //编辑框
            addDialogFormVisible: false,    //新建框
            aboutMFormVisible: false,
            merchant: [],
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.selectAll();
        },
        methods: {
            selectAll() {
                var _this = this;
                axios({
                    method: "get",
                    url: "http://localhost:8080/selectAll"
                }).then(function (resp) {
                    _this.commodityList = resp.data.data;
                })
            },
            handleSuccess(res) {

                console.log(res);
                this.commodity.image = res.data.src
            },
            // handleSuccess(res){
            //     const url = res.imageUrl.slice(8);
            //     let image = 'http://localhost:8080/'+url;
            //     this.commodity.image = image;
            //     console.log(this.commodity.image);
            // },
            handleImageChange(file) {
                console.log('image', file)
                const formData = new FormData();
                formData.append("image", file.raw);

                axios.post("/api/commodity/upload-image", formData)
                    .then(response => {
                        this.commodity.image = response.data.imageUrl;
                    })
                    .catch(error => {
                        console.error(error);
                    });
            },
            /*查看详细*/
            handleLook(row) {
                var _this = this;
                axios({
                    method: "get",
                    url: "http://localhost:8080/selectById/" + row.commodityId
                }).then(function (resp) {
                    if (resp.data.code === 20031) {
                        _this.commodity = resp.data.data;
                        _this.lookDialogFormVisible = true;
                    } else {
                        _this.$message.error(resp.data.msg);
                    }
                })
            },
            /*弹出编辑窗口*/
            handleUpdate(row) {
                var _this = this;
                axios({
                    method: "get",
                    url: "http://localhost:8080/selectById/" + row.commodityId
                }).then(function (resp) {
                    if (resp.data.code === 20031) {
                        _this.commodity = resp.data.data;
                        _this.editDialogFormVisible = true;
                    } else {
                        _this.$message.error(resp.data.msg);
                    }
                })
            },
            /*确认编辑*/
            handleEdit() {
                var _this = this;
                axios({
                    method: "put",
                    url: "http://localhost:8080/update",
                    data: _this.commodity
                }).then(function (resp) {
                    //如果操作成功，关闭弹窗，刷新数据
                    //console.log(resp.data);
                    if (resp.data.code === 20041) {    //成功
                        _this.editDialogFormVisible = false;
                        _this.$message.success(resp.data.msg);
                    } else if (resp.data.code === 20040) {     //失败
                        _this.$message.error(resp.data.msg);
                    } else {
                        _this.$message.error(resp.data.msg);
                    }
                }).finally(function () {
                    _this.selectAll();
                });
            },
            //删除一个
            handleDelete(row) {
                this.$confirm('此操作将删除该条数据，是否继续？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //确定删除
                    var _this = this;
                    axios({
                        method: "get",
                        url: "http://localhost:8080/deleteById/" + row.commodityId
                    }).then(function (resp) {
                        if (resp.data.code === 20021) {    //成功
                            _this.$message.success("删除成功");
                        } else {
                            _this.$message.error(resp.data.msg);
                        }
                    }).finally(function () {
                        _this.selectAll();
                    });
                }).catch(() => {
                    //取消删除
                    this.$message.info("已取消删除");
                });
            },
            //重置表单
            resetForm() {
                this.commodity = {
                    image: ''
                }
            },
            //弹出新建框
            handleCreate() {
                this.addDialogFormVisible = true;
                this.resetForm();   //每次弹框都清除表单里面的数据
            },
            //新建
            handleAdd() {
                var _this = this;
                axios({
                    method: "post",
                    url: "http://localhost:8080/insert",
                    data: _this.commodity
                }).then(function (resp) {
                    //如果操作成功，关闭弹窗，刷新数据
                    //console.log(resp.data);
                    if (resp.data.code === 20011) {    //成功
                        _this.addDialogFormVisible = false;
                        _this.$message.success(resp.data.msg);
                    } else if (resp.data.code === 20010) {     //失败
                        _this.$message.error(resp.data.msg);
                    } else {
                        _this.$message.error(resp.data.msg);
                    }
                }).finally(function () {
                    _this.selectAll();
                });
            },
            //根据商品名称进行模糊查询
            handleSearch() {
                var _this = this;
                axios({
                    method: "get",
                    url: "http://localhost:8080/selectByName?name=" + _this.spoonName,
                }).then(function (resp) {
                    if (resp.data.code === 20031) {
                        _this.commodity = resp.data.data;
                    } else {
                        _this.$message.error(resp.data.msg);
                    }
                })
            },
            //查看商家(管理员)详细信息
            /*aboutM(){
                var _this = this;
                axios({
                    method: "get",
                    url: "http://localhost:8080/merchant/selectByName/" + _this.username,
                }).then(function (resp) {
                    if(resp.data.code === 20031){
                        _this.merchant = resp.data.data;
                        _this.aboutMFormVisible = true;
                    }else {
                        _this.$message.error(resp.data.msg);
                    }
                })
            },*/
            //确认编辑商家信息
            updateM() {
                var _this = this;
                axios({
                    method: "put",
                    url: "http://localhost:8080/merchant/update",
                    data: _this.merchant
                }).then(function (resp) {
                    //如果操作成功，关闭弹窗，刷新数据
                    //console.log(resp.data);
                    if (resp.data.code === 20041) {    //成功
                        _this.aboutMFormVisible = false;
                        _this.$message.success(resp.data.msg);
                    } else if (resp.data.code === 20040) {     //失败
                        _this.$message.error(resp.data.msg);
                    } else {
                        _this.$message.error(resp.data.msg);
                    }
                })
            },
        },
    });
</script>
</html>
